package com.itheima.web.controller;

import com.itheima.web.manager.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserManager userManager;

    // 分页查询
    @GetMapping("/manage/users")
    public ResponseEntity findUserInfoByPage(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                             @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return userManager.findUserInfoByPage(pageNum, pageSize);
    }

    // 用户详情
    @GetMapping("/manage/users/{userId}")
    public ResponseEntity findUserInfoById(@PathVariable Long userId) {
        return userManager.findUserInfoById(userId);
    }

    // 动态列表
    @GetMapping("/manage/messages")
    public ResponseEntity findMovementVoByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "uid", required = false) Long userId,
            Integer state) {
        return userManager.findMovementVoByPage(userId, state, pageNum, pageSize);
    }

    // 动态详情
    @GetMapping("/manage/messages/{publishId}")
    public ResponseEntity findMovementVoById(@PathVariable String publishId){
        return userManager.findMovementVoById(publishId);
    }

    // 动态评论分页
    @GetMapping("/manage/messages/comments")
    public ResponseEntity findCommentVoByPage(
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize,
            @RequestParam("messageID") String publishId){
        return userManager.findCommentVoByPage(pageNum,pageSize,publishId);
    }

    //冻结
    @PostMapping("/manage/users/freeze")
    public ResponseEntity saveFreeze(@RequestBody Map<String,String> map){
        //1.接收参数
        int userId = Integer.parseInt(((map.get("userId"))));//用户id
        int freezingTime = Integer.parseInt(map.get("freezingTime"));//冻结时间
        int freezingRange = Integer.parseInt(map.get("freezingRange"));//冻结范围
        String reasonsForFreezing = map.get("reasonsForFreezing");//冻结原因
        String frozenRemarks = map.get("frozenRemarks");//冻结备注
        //2.调用manager
        return userManager.saveFreeze(userId,freezingTime,freezingRange,reasonsForFreezing,frozenRemarks);
    }

    //解冻
    @PostMapping("/manage/users/unfreeze")
    public ResponseEntity updateFreeze(@RequestBody Map<String,String> map){
        //接收到参数
        int userId = Integer.parseInt(map.get("userId"));
        String reasonsForThawing = map.get("reasonsForThawing");//解冻原因
        //调用manager
        return userManager.updateFreeze(userId,reasonsForThawing);
    }
}
